<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售日期" prop="saleDate">
        <el-date-picker clearable
          v-model="queryParams.saleDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择销售日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
    
        <el-select v-model="queryParams.customerId" placeholder="请选择客户信息">
          <el-option
            v-for="item in this.customerOptions"
            :key="item.customerId"
            :label="item.customerName"
            :value="item.customerId">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['btlfbreed:chickenSalesInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['btlfbreed:chickenSalesInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['btlfbreed:chickenSalesInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['btlfbreed:chickenSalesInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chickenSalesInfoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="saleId" /> -->
      <el-table-column label="销售日期" align="center" prop="saleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户信息" align="center" prop="customerName" />
      <el-table-column label="销售重量(kg)" align="center" prop="saleWeight" />
      <el-table-column label="销售单价(元/kg)" align="center" prop="salePrice" />
      <el-table-column label="销售成本(元/kg)" align="center" prop="saleCost" />
      <el-table-column label="销售金额" align="center" prop="saleAmount" />
      <el-table-column label="客户姓名" align="center" prop="customerName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['btlfbreed:chickenSalesInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['btlfbreed:chickenSalesInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改肉鸡销售对话框 -->
    <el-dialog :title="title" fullscreen="true" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form"  :inline="true"  :model="form" :rules="rules" label-width="120px">
        <el-form-item label="销售日期" prop="saleDate">
          <el-date-picker clearable
            v-model="form.saleDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择销售日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户" prop="customerId">
          <el-select  v-model="form.customerId"  placeholder="请选择客户信息">
          <el-option
            v-for="item in this.customerOptions"
            :key="item.customerId"
            :label="item.customerName"
            :value="item.customerId">
          </el-option>
       </el-select>
        </el-form-item>
        <el-form-item label="销售重量(kg)" prop="saleWeight">
          <el-input v-model="form.saleWeight" placeholder="请输入销售重量(kg)" />
        </el-form-item>
        <el-form-item label="销售单价(元/kg)" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售单价(元/kg)" />
        </el-form-item>
        <el-form-item label="销售成本(元/kg)" prop="saleCost">
          <el-input v-model="form.saleCost" placeholder="请输入销售成本(元/kg)" />
        </el-form-item>
        <!-- <el-form-item label="销售金额" prop="saleAmount">
          <el-input v-model="form.saleAmount" placeholder="请输入销售金额" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">肉鸡出栏信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbChickenOutRegistry">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbChickenOutRegistry">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tbChickenOutRegistryList" :row-class-name="rowTbChickenOutRegistryIndex" @selection-change="handleTbChickenOutRegistrySelectionChange" ref="tbChickenOutRegistry">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="肉鸡" prop="chickenId" width="150">
            <template slot-scope="scope">
              <el-select v-model="form.chickenId" placeholder="请选择肉鸡信息">
          <el-option-group
              v-for="group in checkOptions"
              :key="group.label"
              :label="group.label">
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)">
              </el-option>
          </el-option-group>
       </el-select>
            </template>
          </el-table-column>
          <!-- <el-table-column label="客户" prop="customerId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.customerId" placeholder="请输入客户" />
            </template>
          </el-table-column> -->
          <el-table-column label="出栏日期" prop="outDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.outDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出栏日期" />
            </template>
          </el-table-column>
          <el-table-column label="出栏体重" prop="outWeight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.outWeight" placeholder="请输入出栏体重" />
            </template>
          </el-table-column>
          <el-table-column label="描述" prop="destination" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.destination" placeholder="请输入描述" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChickenSalesInfo, getChickenSalesInfo, delChickenSalesInfo, addChickenSalesInfo, updateChickenSalesInfo,getOptionCustomerselect,getOptionCheckselect } from "@/api/btlfbreed/chickenSalesInfo";

export default {
  name: "ChickenSalesInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbChickenOutRegistry: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 肉鸡销售表格数据
      chickenSalesInfoList: [],
      // 肉鸡出栏表格数据
      tbChickenOutRegistryList: [],
      // 用户选项
      customerOptions: [],
           // 用户选项
      checkOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleDate: null,
        customerId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.initData();
  },
  methods: {
    /** 查询肉鸡销售列表 */
    getList() {
      this.loading = true;
      listChickenSalesInfo(this.queryParams).then(response => {
        this.chickenSalesInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    initData(){
      getOptionCustomerselect().then(response=>{
        this.customerOptions=response.data;
      });
      getOptionCheckselect().then(response=>{
      this.checkOptions=response.data;
    });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        saleId: null,
        saleDate: null,
        customerId: null,
        saleWeight: null,
        salePrice: null,
        saleCost: null,
        saleAmount: null,
        customerName: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.tbChickenOutRegistryList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.saleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.initData();
      this.reset();
      this.open = true;
      this.title = "添加肉鸡销售";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.initData();
      this.reset();
      const saleId = row.saleId || this.ids
      getChickenSalesInfo(saleId).then(response => {
        this.form = response.data;
        this.tbChickenOutRegistryList = response.data.tbChickenOutRegistryList;
        this.open = true;
        this.title = "修改肉鸡销售";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbChickenOutRegistryList = this.tbChickenOutRegistryList;
          if (this.form.saleId != null) {
            updateChickenSalesInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChickenSalesInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const saleIds = row.saleId || this.ids;
      this.$modal.confirm('是否确认删除肉鸡销售编号为"' + saleIds + '"的数据项？').then(function() {
        return delChickenSalesInfo(saleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 肉鸡出栏序号 */
    rowTbChickenOutRegistryIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 肉鸡出栏添加按钮操作 */
    handleAddTbChickenOutRegistry() {
      let obj = {};
      obj.chickenId = "";
      obj.customerId = "";
      obj.outDate = "";
      obj.outWeight = "";
      obj.destination = "";
      obj.remark = "";
      this.tbChickenOutRegistryList.push(obj);
    },
    /** 肉鸡出栏删除按钮操作 */
    handleDeleteTbChickenOutRegistry() {
      if (this.checkedTbChickenOutRegistry.length == 0) {
        this.$modal.msgError("请先选择要删除的肉鸡出栏数据");
      } else {
        const tbChickenOutRegistryList = this.tbChickenOutRegistryList;
        const checkedTbChickenOutRegistry = this.checkedTbChickenOutRegistry;
        this.tbChickenOutRegistryList = tbChickenOutRegistryList.filter(function(item) {
          return checkedTbChickenOutRegistry.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbChickenOutRegistrySelectionChange(selection) {
      this.checkedTbChickenOutRegistry = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('btlfbreed/chickenSalesInfo/export', {
        ...this.queryParams
      }, `chickenSalesInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
